package com.suxin.algorithm.stack;

import java.util.Scanner;

/**
 * @author Tang
 * @classname BracketsStack
 * @description [ [{}]{}[] - 符号匹配 ]
 * @date 2022/4/8 23:24
 */
public class BracketsStack {

    public boolean isBracketOk(String string) {
        MyStackByArray<Character> myStack = new MyStackByArray<>(16);
        char[] chars = string.toCharArray();
        Character pop;
        for (int i = 0; i < chars.length; i++) {
            switch (chars[i]) {
                case '{':
                case '[':
                case '(':
                    myStack.push(chars[i]);
                    break;
                case '}':
                    pop = myStack.pop();
                    if (pop != null && !pop.equals('{')) {
                        return false;
                    }
                    break;
                case ']':
                    pop = myStack.pop();
                    if (pop != null && !pop.equals('[')) {
                        return false;
                    }
                    break;
                case ')':
                    pop = myStack.pop();
                    if (pop != null && !pop.equals('(')) {
                        return false;
                    }
                    break;
                default:
                    break;
            }

        }
        return myStack.isEmpty();
    }

    public static void main(String[] args) {
        BracketsStack bracketsStack = new BracketsStack();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.nextLine();
            System.out.println("匹配结果:" + bracketsStack.isBracketOk(str));
        }
    }

}